﻿/*******************************************************************************
牛牛远程控件的JS部分的核心流程 请勿修改 
*******************************************************************************/

//connstate 
var RemoteConn_None = 0;
var RemoteConn_Connecting = 1;
var RemoteConn_Connected = 2;

//remote status 
var RemoteNone = 0;
var VNCViewer = 1;
var VNCServer = 2;
var FileTransfer = 4;

//回调类型
var eCallbackUnknown = 0;
var eFunctionResult = 1;
var eNotice = 2;

//函数返回值回调，对应于哪个函数 
var eUnknown		= 0;
var eInitEngine		= 1 << 0;	
var eInitRemoteInfo = 1 << 1;
var eSetProxy		= 1 << 2;
var eStartRemote	= 1 << 3;
var eStopRemote		= 1 << 4;
var eSetParam		= 1 << 5;
var eGetStatus		= 1 << 6;
var eDisConnect		= 1 << 7;
var eGetVersion     = 1 << 8;

function setCookie(name,value) 
{ 
	var Days = 300; 
	var exp = new Date(); 
	exp.setTime(exp.getTime() + Days*24*60*60*1000); 
	document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString(); 
} 

//读取cookies 
function getCookie(name) 
{ 
	var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
 
	if(arr=document.cookie.match(reg)) 
		return unescape(arr[2]); 
	else 
		return ""; 
} 
	
var remoteControl = null;
function NiuniuRemoteControl() 
{
    var self = this;
	this.base = new NiuniuControlObject("30111,30112", "NiuniuRemote", "application/x-niuniuremote", "VJdp6JzY5y2AddzTvOKCr326TLSnZEuwXRrYctZBwh0=");
    this.downloadUrl = "http://www.ggniu.cn/download/RemoteInstall.exe";
	this.myId = "";
    this.peerId = "";
    this.myName = "";
    this.peerName = "";
	this.asDriver = 1;
	this.serverAddr = "ggniu.cn";
	this.serverPort = 9999;
	
	this.functionCallbackMap = {};
	this.PluginLoaded = null;
	this.PluginUnload = null;
	this.doShowDownload = null;
	this.OnControlNotice = function(obj){ alert("error");}
	
	this.Init = function()
	{	
		self.base.NiuniuAuthKey = "niuniu";
		
		//设置控件加载完成以及回调函数
		self.base.OnInvokeCallback = self.OnInvokeCallback;
		self.base.PluginLoadedCallback = self.PluginLoadedCallback;
		self.base.OnConnectFailed = self.OnConnectFailed;
		
		//初始化控件 
		self.base.InitNiuniucontrol();
	}
	this.isUseful = function()
	{
		return self.base.isUseful();
	}
	
	this.InitRemote = function(myid, peerid, myname, peername, callback)
	{
		self.InitIdentifer(myid, peerid, myname, peername);
		var controlRet = self.InitRemoteInfo(callback);
		if(controlRet == emcontrolFailed)
		{
			self.ShowDownLoad(true);
		}
		else if(controlRet == emcontrolUnknown)
		{
			self.ShowDownLoad(false);
		}
		return controlRet;
	}
	this.StartControl = function (asViewer, isControl, quality, callback)
	{
		var controlRet = self.StartRemote(asViewer, isControl, quality, callback);
		//从返回值来解析显示  	
		if(controlRet == emcontrolFailed)
		{
			self.ShowDownLoad(true);
		}
		else if(controlRet == emcontrolUnknown)
		{
			self.ShowDownLoad(false);
		}
		return controlRet;
	}
	
	this.ShowDownLoad = function(forceDownload)
	{
		if(self.doShowDownload != null)
		{
			self.doShowDownload(forceDownload);
		}
		else
		{
			base.WriteLog("self.doShowDownload is null.");
		}
	}
	
	this.InitIdentifer = function(myid, peerid, myname, peername)
	{
		self.myId = myid;
		self.peerId = peerid;
		self.myName = myname;
		self.peerName = peername;
		self.asDriver = 0;
		if (self.myId > self.peerId)
		{
			self.asDriver = 1;
			self.base.identifier = self.myId + "_" + self.peerId;
		}
		else
		{
			self.base.identifier = self.peerId + "_" + self.myId;
		}
	}
	
    this.NewControlParamObject = function(type, callback)
    {  
        var obj = new Object(); 
        obj.cmdType = eInitEngine;  
		obj.cmdType |= type;
        obj.authKey = self.base.NiuniuAuthKey;  //	
		obj.identifier = self.base.identifier;
		obj.asDriver = self.asDriver;
		obj.serverAddr = self.serverAddr;
		obj.serverPort = self.serverPort;
		obj.myName = self.myName;
		obj.peerName = self.peerName;
		
		//授权信息 
		obj.clientUrl = window.location.host;
		obj.webcontrolAuth = self.base.webcontrolAuth;
		//此处绑定函数调用类型的回调函数
		if(callback != null && callback != undefined && typeof callback == 'function')
		{
			self.functionCallbackMap[type] = callback;
		}
        return obj; 
    } 

	this.StartEngine = function()
	{
		var obj = new Object();
		obj.authKey = self.base.NiuniuAuthKey;
		obj.cmdType = -1;
		var json = $.toJSON(obj);
        var controlRet = self.base.doInvoke(json, true);
		//从返回值来解析显示  	
		if(controlRet == emcontrolFailed)
		{
			self.ShowDownLoad(true);
		}
		else if(controlRet == emcontrolUnknown)
		{
			self.ShowDownLoad(false);
		}
	}
	this.InitRemoteInfo = function(callback)
	{
		var obj = self.NewControlParamObject(eInitRemoteInfo, callback);
		var json = $.toJSON(obj);		
		
        return self.base.doInvoke(json, true);
	}
	
	this.StartRemote = function(isViewer, isControl, quality, callback)
	{
		var obj = self.NewControlParamObject(eStartRemote, callback);
		obj.isViewer = isViewer;
		obj.isControl = isControl;
		obj.quality = quality;
		var json = $.toJSON(obj);        
        return self.base.doInvoke(json, true);
	}
	
	this.StopRemote = function(callback)
	{
		var obj = self.NewControlParamObject(eStopRemote, callback);		
		var json = $.toJSON(obj);        
        return self.base.doInvoke(json, false);
	}
	
	this.DisConnect = function(callback)
	{
		var obj = self.NewControlParamObject(eDisConnect, callback);		
		var json = $.toJSON(obj);        
        return self.base.doInvoke(json, false);
	}
	
	this.GetStatus = function(callback)
	{
		var obj = self.NewControlParamObject(eGetStatus, callback);
		var json = $.toJSON(obj);
        return self.base.doInvoke(json, false);
	}
	
	this.GetVersion = function(callback)
	{	
		var obj = self.NewControlParamObject(eGetVersion, callback);
		//如果同时加载多个控件，则传此值，用于控制要获取哪个控件的版本号 
		obj.controlName = "niuniuremote";
		var json = $.toJSON(obj);
        return self.base.doInvoke(json, false);			
	}
		
	/*当控件成功加载后回调的的函数，您可以在此控制相应的UI显示  */
	this.PluginLoadedCallback = function(success)
	{
		if(self.PluginLoaded != null)
		{
			self.PluginLoaded(success);
		}
		if(success)
		{
			//如果是上次未完成的命令需要连接上后继续发送的，可以在此处再发一次 
			
		}
	}
	
	/*当JS在经过重试的情况下，还是未连接成功，则会回调此函数，进行UI提示或其他的业务处理  */
	this.OnConnectFailed = function(isReconnect)
	{
		if(isReconnect)
		{
			if(self.PluginUnload != null)
			{
				self.PluginUnload();
			}
		}
		else
		{			
			self.PluginLoadedCallback(false);
		}
	}
		
	
	this.OnFunctionResult = function(obj, content)
	{
		var func = self.functionCallbackMap[obj.callType];		
		delete self.functionCallbackMap[obj.callType];
		if(func != null && func != undefined)
		{
			func(obj);
		}
		return;
	}
	
	/*此处是控件数据交互的回调函数(包含接口返回值以及控件内部主动推送的回调信息)，请分类处理 */
	this.OnInvokeCallback = function(obj, content) 
	{
		if(obj.type < 0)
		{
			//需要重新安装控件
			self.ShowDownLoad(true);
			return;
		}
		switch(obj.type)
		{
			case eFunctionResult:
			{
				self.OnFunctionResult(obj, content);
			}
			break;
			case eNotice:
			{
				self.OnControlNotice(obj);
			}
			break;
			case eCallbackUnknown:
			default:
			break;
		}   
	}
	
	this.GetRemoteStatusString = function(nStatus)
	{
		switch(nStatus)
		{
		case RemoteNone:
			return "未启动";
			break;
		case VNCServer:
			return "正在启动服务或被控制中";
			break;
		case VNCViewer:
			return "正在启动控制或控制中";
			break;
		default:
			break;
		}
		return "未知";
	}

	this.GetConnectStatusString = function(nStatus)
	{
		switch(nStatus)
		{
		case RemoteConn_None:
			return "未连接";
			break;
		case RemoteConn_Connecting:
			return "连接中";
			break;
		case RemoteConn_Connected:
			return "已连接";
			break;
		default:
			break;
		}
		return "未知";
	}
}
